#pragma region License
/************************************************************************
 *                                                                      *
 * Copyright (C) 2010, 2011 Ghoul                                       *
 * All rights reserved.                                                 *
 *                                                                      *
 ************************************************************************/
#pragma endregion

#include "MSvlb.h"

DWORD dwMobSelectAddress = 0x00AFBFD7; // 3 NOP's
DWORD dwClosestMobX = 0;
DWORD dwCharFH = 0;
DWORD dwClosest = 0xFFFFFFFF;

void __declspec(naked) WINAPI MobSelect ( void )
{
    __asm {
        push ebx
        push esi
        mov esi, ecx

        test esi, esi
        je OriginalCode
        push ecx
        mov ecx, [esi+1A0]
        test ecx, ecx
        pop ecx
        je OriginalCode

        cmp esi, [0x00CC0530]
        je PlayerCode
        
PlayerCode:
        push ecx
        mov ecx, [esi+1A0]
        mov [dwCharFH], ecx
        pop ecx
        jmp OriginalCode

MobCode:
        push ebx
        mov ebx, [dwCharFH]
        test ebx, ebx
        pop ebx
        je OriginalCode
        push ecx
        push eax
        mov ecx, [esi+1A0]
        cmp ecx, [dwCharFH]
        je MobCodeC1

MobCodeL1:
        push ebx
        mov ebx, [ecx+64]
        test ebx, ebx
        pop ebx
        jne MobCodeL2
        jmp MobCodeL3

MobCodeL2:
        inc ecx
        cmp // todo - check if closer, etc

MobCodeL3:
        pop eax
        pop ecx
        jmp OriginalCode

MobCodeC1:
        cmp [dwClosest], 0
        je MobCodeC2
        mov ecx, [ecx+160]
        mov [dwClosestMobX], ecx

MobCodeC2:
        pop ecx
        jmp OriginalCode

OriginalCode:
        and dword ptr [ebp-10], 00
    }
}